<template>
    <div class="statistics_box">
        <div class="title">趋势图</div>
        <div class="border"></div>
        <div class="btn_box">
            <ZWRadioButton :list="btnList" @submit="onChangeType" />
            <el-button v-if="state.date?.length" @click="onExportImg(myChartLineRef)">导出</el-button>
        </div>
        <div v-if="state.date?.length" id="echarts" ref="myChartLineRef"></div>
        <div v-else class="none_data">
            <img class="no_img" src="@/assets/noData.png" alt="" />
            <div>暂无数据</div>
        </div>
    </div>
</template>

<script setup lang="ts">
import ZWRadioButton from '@/components/ZWRadioButton.vue';
import { ref, inject, defineExpose, nextTick, watch } from 'vue';
import { export2Img } from '@/utils/utils';
const state = ref({
    date: [], //日期时间
    num_join: [],
    num_retreat: [],
    num_group: []
});
const btnList = ['进群人次', '退群人次', '净增人数'];
const echarts: any = inject('ec');
const pageType = ref(0);
const myChartLineRef = ref(null);
const getEchartsLine = (typeName = '进群人次', v: any = state.value.num_join) => {
    const myChartLine = echarts?.init(myChartLineRef.value);
    const option = {
        color: ['#fa6c00'],
        tooltip: {
            trigger: 'axis',
            axisPointer: {
                type: 'cross',
                label: {
                    show: false
                }
            }
        },
        grid: {
            top: 20,
            bottom: 10,
            left: 32,
            right: 40,
            containLabel: true
        },
        xAxis: {
            type: 'category',
            data: state.value.date,
            boundaryGap: false,
            // axisTick: {
            //   show: false,
            //   alignWithLabel: true
            // },
            offset: 3
        },
        yAxis: {
            type: 'value',
            name: '人',
            minInterval: 1,
            nameGap: 10, // y轴name与横纵坐标轴线的间距
            nameTextStyle: {
                padding: [0, 35, 10, 0] // 加上padding可以调整其位置
            },
            axisTick: {
                show: false
            },
            axisLabel: {
                color: '#969696'
            },
            splitLine: {
                lineStyle: {
                    type: 'line',
                    color: '#e8e8e8' // 分割线颜色
                }
            }
        },
        series: [
            {
                name: typeName,
                type: 'line',
                data: v,
                smooth: true,
                symbol: 'circle',
                symbolSize: 0,
                areaStyle: {
                    normal: {
                        color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
                            { offset: 0, color: 'rgba(247, 222, 211)' },
                            { offset: 0.5, color: 'rgba(251, 230, 223)' },
                            { offset: 1, color: 'rgba(255, 255, 255)' }
                        ])
                    }
                },
                itemStyle: {
                    normal: {
                        borderWidth: 2,
                        color: '#FF9D23'
                    }
                },
                emphasis: {
                    // 鼠标 hover 时的样式
                    itemStyle: {
                        color: '#FF9D23', // hover 圆点颜色
                        borderColor: '#FF9D23', // hover 圆点边框颜色
                        borderWidth: 8
                    }
                }
            }
        ]
    };
    myChartLine.setOption(option);
    nextTick(() => {
        myChartLine.resize();
    });
    window.onresize = function () {
        myChartLine.resize();
    };
};
const onChangeType = (v = 0) => {
    pageType.value = v;
    if (state.value.date?.length) {
        switch (v) {
            case 0:
                getEchartsLine('进群人次', state.value.num_join);
                break;
            case 1:
                getEchartsLine('退群人次', state.value.num_retreat);
                break;
            case 2:
                getEchartsLine('净增人数', state.value.num_group);
                break;
        }
    }
};
const onExportImg = (chartEl: any) => {
    export2Img(chartEl, btnList[pageType.value]);
};
watch(
    state,
    v => {
        if (v.date.length) {
            nextTick(() => {
                onChangeType(pageType.value);
            });
        }
    },
    { deep: true }
);
defineExpose({
    state
});
</script>

<style lang="scss" scoped>
.statistics_box {
    padding: 20px 26px;
    margin-bottom: 16px;
    background: #fff;
    box-shadow: 0 4px 4px 0 rgb(240 240 240 / 50%);
    border: 1px solid #eaeaea;
    font-size: 14px;
    color: #33302d;

    .title {
        display: flex;
        justify-content: space-between;
        padding-bottom: 20px;
        border-bottom: 1px solid #eaeaea;
        font-size: 16px;
    }

    .btn_box {
        display: flex;
        justify-content: space-between;
        padding: 16px 0;
    }

    #echarts {
        display: flex;
        justify-content: center;
        height: 320px;
    }
}

.circle_box {
    display: flex;
    margin-bottom: 16px;

    .circle {
        flex: 1;
        padding: 20px 26px;
        margin-right: 20px;
        background: #fff;
        box-shadow: 0 4px 4px 0 rgb(240 240 240 / 50%);
        border: 1px solid #eaeaea;

        .circle_title {
            display: flex;
            justify-content: space-between;
            align-items: center;
            padding-bottom: 8px;
            border-bottom: 1px solid #eaeaea;
        }
    }

    .margin {
        margin-right: 0;
    }
}

.none_data {
    margin: 100px 0;
    display: flex;
    justify-content: center;
    align-items: center;
    flex-direction: column;
    color: #909399;

    img {
        transform: translateX(15%);
    }
}
</style>
